<?php
class find extends common{
    var  $table_name = 'informations';
    var   $table_channelname  = 'informations_channels';
    private $companies_table = "companies";
	private $companies_categories_table = "companies_categories";
    private $companies_detail_title_table = "companies_detail_title";
	private $companies_detail_table = "companies_detail";
    //获取频道
    function channelList() {
         $sql = $this->table_channelname;
         $result= $this->getList($sql);
         $msg = '请求成功';
	 $error = '0';
	return array('error'=>$error,'msg'=>$msg,'list'=>$result['result']
                );	
    }
	
    /**
	 *获取资讯列表
	 * @param	string	$type
	 * @param	string	$key
	 * @param	int		$num
	 * @param	int		$page
	 * @return	array 
	*/
 function informationList($param) {
          extract($param);
        /* @var $channel_id int */
        $sql = $this->table_name." where channel_id = '".$channel_id."'";
         $result= $this->getList($sql,'id,title,simtitle,date,img',$num,$page);
        
         $msg = '请求成功';
	 $error = '0';
	return array('error'=>$error,'msg'=>$msg,'list'=>$result['result']
                );	
    }
 function informationDetail($param) {
          extract($param);
        /* @var $channel_id int */
        $sql = $this->table_name." where id = '".$information_id."'";
         $result= $this->getInfo($sql,'title,author,content,date,froms');
         if (is_array($result)) {
          $str =$result['content'];
          $str = str_replace("<img src=","%img", $str);
          $str = str_replace("alt=","<", $str);
          $str = str_replace("</p>","|", $str);
          $qian=array(" ",'"',"　","\t","\n","\r");
          $str =   str_replace($qian,'',$str);
          $str =strip_tags( $str); 
          $str =   str_replace('/','%&',$str);
          $result['content'] = $str;
         }
         $msg = '请求成功';
	 $error = '0';
	return array('error'=>$error,'msg'=>$msg,'result'=>$result
                );	
    }


	
	//资讯搜索
	function searchInformation($param){
		extract($param);
		if($_GET['submit']='搜索'){
			if($keywords!='' && $keywords!=null){
				$sql=$this->table_name." where title like '%".$keywords."%' or simtitle like '%".$keywords."%'";
			    $select=$this->getList($sql,"id,title,simtitle,date,img",$debug='0');
			}
		}
		return $select;
	}
	//多功能搜索
	function searchMuch($param){
		extract($param);
		if($_GET['submit']='搜索'){
	
		$company_name = isset($company_name) ? trim($company_name) : '';//获取公司名称
		$star= isset($star) ? trim($star) : ''; //获取公司星级
		$province= isset($province) ? trim($province) : '';//获取省
		$city= isset($city) ? trim($city) : '';//获取城市 
		$county= isset($county) ? trim($county) : '';//获取城市
		$secondcate= isset($secondcate) ? trim($secondcate) : '';
		$where = ' where c.id>0';
		if(!empty($company_name)){
		   $where .=' and c.title like "%'.$company_name.'%"';
		}
		if(!empty($star)){
		   $where .=' and c.star=\''.$star.'\'';
		}
		if(!empty($province)){
		   $where .=' and c.province like "%'.$province.'%"';
		}
		if(!empty($city)){
		   $where .=' and c.cirty like "%'.$city.'%"';
		}
		if(!empty($county)){
		   $where .=' and c.county like "%'.$county.'%"';
		}

		if(!empty($secondcate)){
		   $where .=' and g.title like "%'.$secondcate.'%"';
		}
			if($company_name!='' || $star!='' || $province!='' || $city!='' || $county!='' || $secondcate!='' || $firstcate!=''){
				if($firstcate!="" && $firstcate!=null){
				//根据一级分类查询
				$sql1=$this->companies_categories_table."  where title like '%".$firstcate."%'";
				$row=$this->getList($sql1,'sort',$debug='1');
				//var_dump($sql1);
					if(is_array($row['result'])){
					foreach($row['result'] as $keys=>$values){	
					}
					//var_dump($values['sort']);
						if(!empty($values['sort'])){
						   $where .=" and g.upid=".$values['sort'];
						}
					}
				}
					$sql=$this->companies_table." c left join ".$this->companies_categories_table." g on c.upid=g.sort".$where;
					$row=$this->getList($sql,'c.title,c.id,c.star,c.img,c.province,c.cirty,c.county',$num,$page,$debug='0');
					//var_dump($row);
					if(is_array($row['result'])){
						foreach($row['result'] as $k=>$v){
							$sql = $this->companies_detail_table." d
									left join ".$this->companies_detail_title_table." t on d.title_id=t.id
									where d.company_id='".$v['id']."'";
							$info = $this->getList($sql,'t.id_marking,d.content');
							if(is_array($info['result'])){
								foreach($info['result'] as $key=>$val){
									$v[$val['id_marking']] = $val['content'];
								}
							}
							$res['result'][$k] = $v;						
						}
						
					}
					return $res;
			}else{
					$sql=$this->companies_table." c left join ".$this->companies_categories_table." g on c.upid=g.sort".$where;
					$row=$this->getList($sql,'c.title,c.id,c.star,c.img,c.province,c.cirty,c.county',$debug='0');
					//var_dump($row);
					if(is_array($row['result'])){
						foreach($row['result'] as $k=>$v){
							$sql = $this->companies_detail_table." d
									left join ".$this->companies_detail_title_table." t on d.title_id=t.id
									where d.company_id='".$v['id']."'";
							$info = $this->getList($sql,'t.id_marking,d.content');
							if(is_array($info['result'])){
								foreach($info['result'] as $key=>$val){
									$v[$val['id_marking']] = $val['content'];
								}
							}
							$res['result'][$k] = $v;						
						}
						
					}
					return $res;
				
			}
						
		}
		
	
	}
}